//
//  EGLineChartView.m
//  MotionLaunchDemo
//
//  Created by 程登伟 on 16/9/17.
//  Copyright © 2016年 程登伟. All rights reserved.
//

#import "EGLineChartView.h"

#define borderWidth 20

@interface EGLineChartView ()

@property (nonatomic, strong) NSArray *XDataArr;
@property (nonatomic, strong) NSArray *YDataArr;
@property (nonatomic, strong) NSMutableArray *pointArr;
@property (nonatomic, strong) CAShapeLayer *lineChartLayer;

@end


@implementation EGLineChartView


- (void)drawRect:(CGRect)rect {

/*
    // 坐标轴
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1);
    [[UIColor blueColor] set];
    CGContextMoveToPoint(context, borderWidth, borderWidth);
    CGContextAddLineToPoint(context, borderWidth, rect.size.height - borderWidth);
    CGContextAddLineToPoint(context, rect.size.width - borderWidth, rect.size.height - borderWidth);
    CGContextStrokePath(context);
    self.isLineDisplay = YES;
*/
    
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetLineWidth(context, 1);
//    [[UIColor blueColor] set];
//    
//    CGContextMoveToPoint(context, borderWidth, self.frame.size.height - borderWidth);
//    NSLog(@"arr = %@",self.pointArr);
//    for (int i = 0; i < self.pointArr.count; i ++) {
//        CGPoint point = CGPointFromString([self.pointArr objectAtIndex:i]);
//        CGFloat pointX = point.x;
//        CGFloat pointY = point.y;
//        
//        UILabel *Xlabel = [self findXLabelWith:pointX];
//        UILabel *Ylabel = [self findYLabelWith:pointY];
//        
//        NSLog(@"x = %f,y = %f,pointX = %f,pointY = %f",Xlabel.center.x,Ylabel.center.y,pointX,pointY);
//        CGContextAddLineToPoint(context, Xlabel.center.x, Ylabel.center.y);
//        
//    }
//    
//    CGContextStrokePath(context);
    
}


-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        [self initViewXY];
        
        [self initDataXY];
        
    }
    return self;
    
}

+ (instancetype)showLineChartViewFrame:(CGRect )rect dataArr:(NSArray *)dataArr{
    
    EGLineChartView *lineChartView = [[EGLineChartView alloc] initWithFrame:rect];
    lineChartView.pointArr = [[NSMutableArray alloc] initWithArray:dataArr];
    [lineChartView drawLine];
    return lineChartView;
    
}


- (void) initViewXY{
    
    UIImageView *axisXImage = [[UIImageView alloc] initWithFrame:CGRectMake(borderWidth, self.frame.size.height - borderWidth, self.frame.size.width - borderWidth*2, 2)];
    axisXImage.backgroundColor = [UIColor blueColor];
    [self addSubview:axisXImage];
    
    UIImageView *axisYImage = [[UIImageView alloc] initWithFrame:CGRectMake(borderWidth, borderWidth, 2, self.frame.size.height - borderWidth*2)];
    axisYImage.backgroundColor = [UIColor blueColor];
    [self addSubview:axisYImage];
    
}


- (void) initDataXY{
    
    self.XDataArr = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
    self.YDataArr = [[NSArray alloc] initWithArray:self.XDataArr];
    
    CGFloat Xspace = 0;
    CGFloat XlabelWidth = 35;
    Xspace = (self.frame.size.width - borderWidth*2 - XlabelWidth*self.XDataArr.count)/(self.XDataArr.count - 1)*1.0;

    
    CGFloat Yspace = 0;
    CGFloat YlabelHeight = 35;
    Yspace = (self.frame.size.height - borderWidth*2 - YlabelHeight*self.YDataArr.count)/(self.YDataArr.count - 1)*1.0;
    
    

    // x轴坐标数据
    for (int i = 0; i < self.XDataArr.count; i ++) {
        
        UILabel *XLabel = [[UILabel alloc] initWithFrame:CGRectMake(borderWidth + (Xspace + XlabelWidth)*i, self.frame.size.height - borderWidth, XlabelWidth, borderWidth)];
        XLabel.text = [self.XDataArr objectAtIndex:i];
        XLabel.tag = 1000 + i;
        XLabel.textAlignment = NSTextAlignmentCenter;
        XLabel.font = [UIFont systemFontOfSize:13];
        XLabel.transform = CGAffineTransformMakeRotation(M_PI*0.3);
        [self addSubview:XLabel];
        
    }
    
    
    // y轴坐标数据
    for (int i = 0; i < self.YDataArr.count; i ++) {
        
        UILabel *YLabel = [[UILabel alloc] initWithFrame:CGRectMake(-borderWidth, borderWidth + (Yspace + YlabelHeight)*i, YlabelHeight, borderWidth)];
        YLabel.text = [self.YDataArr objectAtIndex:(self.YDataArr.count - i - 1)];
        YLabel.tag = 2000 + i;
        YLabel.textAlignment = NSTextAlignmentRight;
        YLabel.font = [UIFont systemFontOfSize:13];
        YLabel.transform = CGAffineTransformMakeRotation(M_PI*0.3);
        [self addSubview:YLabel];
        
    }
    
    
}


-(void)drawLine{
    
    NSInteger minCount = MIN(self.XDataArr.count, self.YDataArr.count);
    if (self.pointArr.count > minCount) {
        [self.pointArr removeObjectsInRange:NSMakeRange(minCount, self.pointArr.count - minCount)];
    }

    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(borderWidth, self.frame.size.height - borderWidth)];
    for (int i = 0; i < self.pointArr.count; i ++) {
        CGPoint point = CGPointFromString([self.pointArr objectAtIndex:i]);
        CGFloat pointX = point.x;
        CGFloat pointY = point.y;
        
        UILabel *Xlabel = [self findXLabelWith:pointX];
        UILabel *Ylabel = [self findYLabelWith:pointY];
        
        NSLog(@"x = %f,y = %f,pointX = %f,pointY = %f",Xlabel.center.x,Ylabel.center.y,pointX,pointY);
        [path addLineToPoint:CGPointMake(Xlabel.center.x, Ylabel.center.y)];
        
    }
    
    shapeLayer.path = path.CGPath;
    
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.lineWidth = 2.0f;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    
    [self.layer addSublayer:shapeLayer];
    
    CABasicAnimation *pathAnima = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnima.duration = 3.0f;
    pathAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pathAnima.fromValue = [NSNumber numberWithFloat:0.0f];
    pathAnima.toValue = [NSNumber numberWithFloat:1.0f];
    pathAnima.fillMode = kCAFillModeForwards;
    pathAnima.removedOnCompletion = NO;
    [shapeLayer addAnimation:pathAnima forKey:@"strokeEndAnimation"];
    
    
}



-(UILabel *)findXLabelWith:(CGFloat)count{
    
    
    for (int i = 0; i < self.XDataArr.count; i ++) {
        if (count == [[self.XDataArr objectAtIndex:i] integerValue]) {
            UILabel *label = [self viewWithTag:1000 + i];
            return label;
        }
    }
    
    return nil;
}

-(UILabel *)findYLabelWith:(CGFloat)count{
    
    
    for (int i = 0; i < self.YDataArr.count; i ++) {
        if (count == [[self.YDataArr objectAtIndex:i] integerValue]) {
            UILabel *label = [self viewWithTag:2000 + (self.YDataArr.count - 1 - i)];
            return label;
        }
    }
    
    return nil;
}


@end
